package hhb.game.zhualaoer.core.dao;

import hhb.game.zhualaoer.core.enums.HandType;
import hhb.game.zhualaoer.core.enums.Rank;
import hhb.game.zhualaoer.core.enums.Suit;

import java.util.*;

public class PokerHand implements Comparable<PokerHand> {
    private final List<Card> cards;
    private HandType handType;
    private List<Card> sortedCards;
    private Rank pairRank; // 对子的点数
    private Card singleCard; // 单张牌（用于对子比较）

    public PokerHand(List<Card> cards) {
        if (cards.size() != 3) {
            throw new IllegalArgumentException("手牌必须是3张");
        }
        this.cards = new ArrayList<>(cards);
        analyzeHand();
    }

    private void analyzeHand() {
        // 对手牌进行排序
        sortedCards = new ArrayList<>(cards);
        sortedCards.sort(Comparator.reverseOrder());

        // 判断是否是三张一样
        if (isThreeOfAKind()) {
            handType = HandType.THREE_OF_A_KIND;
            return;
        }

        // 判断是否是同花顺
        if (isStraightFlush()) {
            handType = HandType.STRAIGHT_FLUSH;
            return;
        }

        // 判断是否是同花
        if (isFlush()) {
            handType = HandType.FLUSH;
            return;
        }

        if (isStraight()) {
            handType = HandType.STRAIGHT;
            return;
        }

        // 判断是否是对子
        if (isPair()) {
            handType = HandType.PAIR;
            return;
        }

        // 单张
        handType = HandType.HIGH_CARD;
    }

    private boolean isThreeOfAKind() {
        return sortedCards.get(0).getRank() == sortedCards.get(1).getRank() &&
                sortedCards.get(1).getRank() == sortedCards.get(2).getRank();
    }

    private boolean isStraightFlush() {
        return isFlush() && isStraight();
    }

    private boolean isFlush() {
        Suit suit = sortedCards.get(0).getSuit();
        return sortedCards.get(1).getSuit() == suit && sortedCards.get(2).getSuit() == suit;
    }

    private boolean isStraight() {
        // 检查普通顺子
        boolean isNormalStraight = true;
        for (int i = 0; i < sortedCards.size() - 1; i++) {
            if (sortedCards.get(i).getRank().getValue()
                    != sortedCards.get(i + 1).getRank().getValue() + 1) {
                isNormalStraight = false;
                break;
            }
        }
        if (isNormalStraight) return true;

        // 检查A-2-3顺子
        if (sortedCards.get(0).getRank() == Rank.ACE &&
                sortedCards.get(1).getRank() == Rank.THREE &&
                sortedCards.get(2).getRank() == Rank.TWO) {
            return true;
        }

        return false;
    }

    private boolean isPair() {
        if (sortedCards.get(0).getRank() == sortedCards.get(1).getRank()) {
            pairRank = sortedCards.get(0).getRank();
            singleCard = sortedCards.get(2);
            return true;
        } else if (sortedCards.get(1).getRank() == sortedCards.get(2).getRank()) {
            pairRank = sortedCards.get(1).getRank();
            singleCard = sortedCards.get(0);
            return true;
        }
        return false;
    }

    @Override
    public int compareTo(PokerHand other) {
        // 比较牌型
        int typeCompare = this.handType.compareTo(other.handType);
        if (typeCompare != 0) {
            return typeCompare;
        }

        // 处理相同牌型的比较
        if(handType.equals(HandType.STRAIGHT_FLUSH) || handType.equals(HandType.THREE_OF_A_KIND)
                || handType.equals(HandType.HIGH_CARD) || handType.equals(HandType.FLUSH)
                    || handType.equals(HandType.STRAIGHT)
        ){
            // 前面进行降序排序，这里先从大牌开始比
            int maxCard = compareCardsByRank(this.sortedCards, other.sortedCards);
            if (maxCard != 0) return maxCard;

            //全部一样时比较最大牌的花色
            return this.sortedCards.get(0).getSuit().compareTo(other.sortedCards.get(0).getSuit());
        }else if(handType.equals(HandType.PAIR)){
            // 比较对子大小
            Rank thisPairRank = getPairRank(this.sortedCards);
            Rank otherPairRank = getPairRank(other.sortedCards);
            int pairCompare = thisPairRank.compareTo(otherPairRank);
            if (pairCompare != 0) return pairCompare;

            // 比较单张大小
            Card thisSingle = getSingleCard(this.sortedCards);
            Card otherSingle = getSingleCard(other.sortedCards);
            int SingleCompare = thisSingle.compareToWithRank(otherSingle);
            if (SingleCompare != 0) return SingleCompare;
            //全部一样时比较最大牌的花色
            return this.sortedCards.get(0).getSuit().compareTo(other.sortedCards.get(0).getSuit());
        }
        return 0;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("手牌: ");
        for (Card card : sortedCards) {
            sb.append(card).append(" ");
        }
        sb.append("牌型: ").append(handType);
        return sb.toString();
    }

    public HandType getHandType() {
        return handType;
    }
    // 辅助方法：获取对子的牌面
    private Rank getPairRank(List<Card> cards) {
        if (cards.get(0).getRank() == cards.get(1).getRank()) {
            return cards.get(0).getRank();
        } else {
            return cards.get(1).getRank(); // 对子是第二和第三张牌
        }
    }

    // 辅助方法：获取单张牌
    private Card getSingleCard(List<Card> cards) {
        if (cards.get(0).getRank() == cards.get(1).getRank()) {
            return cards.get(2); // 单张是第三张
        } else {
            return cards.get(0); // 单张是第一张
        }
    }

    // 辅助方法：按牌面比较两组牌
    private int compareCardsByRank(List<Card> cards1, List<Card> cards2) {
        for (int i = 0; i < cards1.size(); i++) {
            int compare = cards1.get(i).compareToWithRank(cards2.get(i));
            if (compare != 0) return compare;
        }
        return 0;
    }


}